//
//  QJMineBankVC.m
//  QuanJieShopProject
//
//  Created by 刘中义 on 2018/2/26.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJMineBankVC.h"
#import "QJMineBankCell.h"
#import "QJMineEditBankVC.h"
@interface QJMineBankVC () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *bankView;
@property (nonatomic, strong) UITableView *bankTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@end

@implementation QJMineBankVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupAddressUIAction];
    self.automaticallyAdjustsScrollViewInsets = NO;
}
#pragma mark -- 界面布局
- (void)setupAddressUIAction{
    self.title = @"我的银行卡";
    self.bankTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT - 115) style:UITableViewStylePlain];
    [self.bankView addSubview:self.bankTableView];
    self.bankTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.bankTableView.delegate = self;
    self.bankTableView.dataSource = self;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self bankDataRequestAction];
    //控件不被导航栏遮挡
    self.navigationController.navigationBar.translucent = NO;
}
#pragma mark -- 数据请求方法
- (void)bankDataRequestAction{
    [WWNetRequest fetchBankCardWithParameters:nil success:^(id response) {
        NSMutableArray *array = [NSMutableArray array];
        if (response) {
            [response enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                MineBankModel *model = [[MineBankModel alloc]initWithDictionary:obj error:nil];
                [array addObject:model];
            }];
        }
        self.dataSourceArray = array;
        [self.bankTableView reloadData];
    } failure:^(NSDictionary *error) {
        
        
    }];
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cellID";
    QJMineBankCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"QJMineBankCell" owner:self options:nil]firstObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    MineBankModel *model = self.dataSourceArray[indexPath.row];
    [cell setupCellUIAction];
    [cell cellUpdateWithModel:model];
    return cell;
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MineBankModel *model = self.dataSourceArray[indexPath.row];
    if ([self.typeStr isEqualToString:@"mine"]) {
        //从个人中心跳转过来的界面
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"选择操作类型" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *editAction = [UIAlertAction actionWithTitle:@"编辑" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self editBankCardWithModel:model];
        }];
        UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self deleteBankCardWithModel:model];
        }];
        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"设为默认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self setDefaultBankCardWithModel:model];
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        NSString *isDefault = model.is_default;
        [alert addAction:editAction];
        [alert addAction:deleteAction];
        if (![isDefault isEqualToString:@"1"]) {
            [alert addAction:defaultAction];
        }
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        //从别的地方跳转过来的界面
        [self getOneBankCardWithModel:model];
    }
    
}
#pragma mark -- 添加银行卡
- (IBAction)addBankBtnClickAction:(UIButton *)sender {
    QJMineEditBankVC *VC = [[QJMineEditBankVC alloc]init];
    KPushViewControllerAction(VC);
}
#pragma mark -- 设为默认银行卡
- (void)setDefaultBankCardWithModel:(MineBankModel *)model{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:model.id forKey:@"id"];
    [WWNetRequest fetchSetDefaultBankCardWithParameters:dict success:^(id response) {
        if (response) {
            [self bankDataRequestAction];
        }
    } failure:^(NSDictionary *error) {
        
    }];
}
#pragma mark -- 删除银行卡
- (void)deleteBankCardWithModel:(MineBankModel *)model{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:model.id forKey:@"id"];
    [WWNetRequest fetchDeleteBankCardWithParameters:dict success:^(id response) {
        if (response) {
            [self bankDataRequestAction];
        }
    } failure:^(NSDictionary *error) {
        
    }];
}
#pragma mark -- 编辑银行卡
- (void)editBankCardWithModel:(MineBankModel *)model{
    QJMineEditBankVC *VC = [[QJMineEditBankVC alloc]init];
    VC.model = model;
    KPushViewControllerAction(VC);
}
#pragma mark -- 获得一个银行卡
- (void)getOneBankCardWithModel:(MineBankModel *)model{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:model.id forKey:@"id"];
    [WWNetRequest fetchBankCardWithParameters:dict success:^(id response) {
        
    } failure:^(NSDictionary *error) {
        
    }];
}
#pragma mark -- 懒加载
- (NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
